package com.wyx.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wyx.Common.R;
import com.wyx.DTO.UserDto;
import com.wyx.Pojo.User;
import com.wyx.Service.UserService;
import com.wyx.Utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 客户端发送验证码功能
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> getMessage(@RequestBody User user, HttpSession session) {
        String phone = user.getPhone();
        if (phone.length() == 11) {
            String code = ValidateCodeUtils.generateValidateCode(6).toString();
            System.out.println(phone+"的验证码是:"+code);
//          SendSms.Sendmessage(phone,code);//发送验证码到手机里
            //先删除之前的验证码
            redisTemplate.delete(phone);
            //将手机号跟验证码存到redis 有效期为5分钟
            redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
            return R.success("发送验证码成功");
        }
        //发送信息给我的手机
        return R.error("发送验证码失败");
    }

    /**
     * 客户端退出app功能
     * @param request
     * @return
     */
    @PostMapping("/loginout")
    public R<String> loginout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }

    /**
     * 客户端登陆功能
     * @param userDto
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody UserDto userDto, HttpServletRequest request) {
        //获取存在Session的验证码
//        String code1 = (String) request.getSession().getAttribute("code"); 过时了
        //获取存在redis的验证码
        String code1 = (String) redisTemplate.opsForValue().get(userDto.getPhone());
        System.out.println(code1);
        if (code1 == null) {
            return R.error("手机号码输入有误");
        }
        //获取电话号码
        String phone = userDto.getPhone();
        //获取用户输入的验证码
        String code = userDto.getCode();
        //验证码匹配
        if (code1.equals(code)) {
            //验证码正确
            //如果存在用户就直接登录 如果没有就创建用户
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getPhone, phone);
            User one = userService.getOne(wrapper);
            //如果是新用户
            if (one == null) {
                one = new User();
                one.setName(phone);
                one.setPhone(phone);
                one.setStatus(1);
                userService.save(one);
//                System.out.println(one);
                if (request.getSession().getAttribute("user")!=null){
                    request.getSession().removeAttribute("user");
                }
//                User user = userService.getOne(wrapper);
                request.getSession().setAttribute("user", one);
//                return R.success(user);
            } else {
                //如果是老用户
                if (request.getSession().getAttribute("user")!=null){
                    request.getSession().removeAttribute("user");
                }
//                User user = userService.getOne(wrapper);
                request.getSession().setAttribute("user", one);
            }
            //删除验证码
            redisTemplate.delete(phone);
            return R.success(one);
        } else {
            //发送验证码失败信息给用户
            return R.error("验证码错误或者手机号输入有误");
        }
    }


}
